package com.google.screen;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

public class ConnectionDetector 
{
	private Context context;
	
	public ConnectionDetector(Context context)
	{
		this.context = context;
	}	
	
	public boolean isConnectingToInternet()
	{
		//kiem tra ket noi internet trong Network Provider
		ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null)
		{
			//lay thong tin ket noi Network
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null)
			{
				for (int i = 0; i < info.length; i++) 
				{
					//kiem tra trai thai ket noi
					if (info[i].getState() == NetworkInfo.State.CONNECTED)
						return true;
				}
			}
		}
		return false;
	}
	
	public void showSettingsAlert() 
    {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
 
        //tieu de dialog
        alertDialog.setTitle("Warning!");
 
        //noi dung dialog
        alertDialog.setMessage("You don't have internet connection. Do you want to go to settings menu?");
 
        //di den Settings Options khi nhan OK
        alertDialog.setPositiveButton("Settings",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                    }
                });
 
        //huy khi nhan cancel
        alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        dialog.cancel();
                    }
                });
        //hien thi dialog
        alertDialog.show();
    }
}
